# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\jinja2\asyncsupport.py
"""
    jinja2.asyncsupport
    ~~~~~~~~~~~~~~~~~~~

    Has all the code for async support which is implemented as a patch
    for supported Python versions.

    :copyright: (c) 2017 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
"""
import sys, asyncio, inspect
from functools import update_wrapper
from jinja2.utils import concat, internalcode, Markup
from jinja2.environment import TemplateModule
from jinja2.runtime import LoopContextBase, _last_iteration

async def concat_async(async_gen):
    rv = []

    def collect--- This code section failed: ---

  25       0  SETUP_LOOP           62  'to 62'
           2  LOAD_DEREF               'async_gen'
           4  GET_AITER        
           6  LOAD_CONST               None
           8  YIELD_FROM       
          10  SETUP_EXCEPT         24  'to 24'
          12  GET_ANEXT        
          14  LOAD_CONST               None
          16  YIELD_FROM       
          18  STORE_FAST               'event'
          20  POP_BLOCK        
          22  JUMP_FORWARD         46  'to 46'
        24_0  COME_FROM_EXCEPT     10  '10'
          24  DUP_TOP          
          26  LOAD_GLOBAL              'StopAsyncIteration'
          28  COMPARE_OP               'exception-match'
          30  POP_JUMP_IF_FALSE    44  'to 44'
          32  POP_TOP          
          34  POP_TOP          
          36  POP_TOP          
          38  POP_EXCEPT       
          40  POP_BLOCK        
          42  JUMP_ABSOLUTE        62  'to 62'
          44  END_FINALLY      
        46_0  COME_FROM            22  '22'

  26      46  LOAD_DEREF               'rv'
          48  LOAD_ATTR                'append'
          50  LOAD_FAST                'event'
          52  CALL_FUNCTION_1       1  ''
          54  POP_TOP          
          56  JUMP_BACK            10  'to 10'
          58  POP_BLOCK        
          60  JUMP_ABSOLUTE        62  'to 62'
        62_0  COME_FROM_LOOP        0  '0'

Parse error at or near `JUMP_ABSOLUTE' instruction at offset 60

    await collect()
    return concat(rv)


def generate_async--- This code section failed: ---

  32       0  LOAD_GLOBAL              'dict'
           2  LOAD_FAST                'args'
           4  LOAD_FAST                'kwargs'
           6  CALL_FUNCTION_EX_KW     1  ''
           8  STORE_FAST               'vars'

  33      10  SETUP_EXCEPT         86  'to 86'

  34      12  SETUP_LOOP           82  'to 82'
          14  LOAD_FAST                'self'
          16  LOAD_ATTR                'root_render_func'
          18  LOAD_FAST                'self'
          20  LOAD_ATTR                'new_context'
          22  LOAD_FAST                'vars'
          24  CALL_FUNCTION_1       1  ''
          26  CALL_FUNCTION_1       1  ''
          28  GET_AITER        
          30  LOAD_CONST               None
          32  YIELD_FROM       
          34  SETUP_EXCEPT         48  'to 48'
          36  GET_ANEXT        
          38  LOAD_CONST               None
          40  YIELD_FROM       
          42  STORE_FAST               'event'
          44  POP_BLOCK        
          46  JUMP_FORWARD         70  'to 70'
        48_0  COME_FROM_EXCEPT     34  '34'
          48  DUP_TOP          
          50  LOAD_GLOBAL              'StopAsyncIteration'
          52  COMPARE_OP               'exception-match'
          54  POP_JUMP_IF_FALSE    68  'to 68'
          56  POP_TOP          
          58  POP_TOP          
          60  POP_TOP          
          62  POP_EXCEPT       
          64  POP_BLOCK        
          66  JUMP_ABSOLUTE        82  'to 82'
          68  END_FINALLY      
        70_0  COME_FROM            46  '46'

  35      70  LOAD_FAST                'event'
          72  YIELD_VALUE      
          74  POP_TOP          
          76  JUMP_BACK            34  'to 34'
          78  POP_BLOCK        
          80  JUMP_ABSOLUTE        82  'to 82'
        82_0  COME_FROM_LOOP       12  '12'
          82  POP_BLOCK        
          84  JUMP_FORWARD        114  'to 114'
        86_0  COME_FROM_EXCEPT     10  '10'

  36      86  DUP_TOP          
          88  LOAD_GLOBAL              'Exception'
          90  COMPARE_OP               'exception-match'
          92  POP_JUMP_IF_FALSE   112  'to 112'
          94  POP_TOP          
          96  POP_TOP          
          98  POP_TOP          

  37     100  LOAD_GLOBAL              'sys'
         102  LOAD_ATTR                'exc_info'
         104  CALL_FUNCTION_0       0  ''
         106  STORE_FAST               'exc_info'
         108  POP_EXCEPT       
         110  JUMP_FORWARD        118  'to 118'
         112  END_FINALLY      
       114_0  COME_FROM            84  '84'

  39     114  LOAD_CONST               None
         116  RETURN_VALUE     
       118_0  COME_FROM           110  '110'

  40     118  LOAD_FAST                'self'
         120  LOAD_ATTR                'environment'
         122  LOAD_ATTR                'handle_exception'
         124  LOAD_FAST                'exc_info'
         126  LOAD_CONST            1  True
         128  CALL_FUNCTION_2       2  ''
         130  YIELD_VALUE      
         132  POP_TOP          

Parse error at or near `JUMP_ABSOLUTE' instruction at offset 80


def wrap_generate_func(original_generate):

    def _convert_generator(self, loop, args, kwargs):
        async_gen = self.generate_async(*args, **kwargs)
        try:
            while True:
                yield loop.run_until_complete(async_gen.__anext__())

        except StopAsyncIteration:
            pass

    def generate(self, *args, **kwargs):
        if not self.environment.is_async:
            return original_generate(self, *args, **kwargs)
        else:
            return _convert_generator(self, asyncio.get_event_loop(), args, kwargs)

    return update_wrappergenerateoriginal_generate


async def render_async(self, *args, **kwargs):
    if not self.environment.is_async:
        raise RuntimeError('The environment was not created with async mode enabled.')
    vars = dict(*args, **kwargs)
    ctx = self.new_context(vars)
    try:
        return await (concat_async(self.root_render_func(ctx)))
    except Exception:
        exc_info = sys.exc_info()

    return self.environment.handle_exceptionexc_infoTrue


def wrap_render_func(original_render):

    def render(self, *args, **kwargs):
        if not self.environment.is_async:
            return original_render(self, *args, **kwargs)
        else:
            loop = asyncio.get_event_loop()
            return loop.run_until_complete(self.render_async(*args, **kwargs))

    return update_wrapperrenderoriginal_render


def wrap_block_reference_call(original_call):

    @internalcode
    def async_call(self):
        rv = await (concat_async(self._stack[self._depth](self._context)))
        if self._context.eval_ctx.autoescape:
            rv = Markup(rv)
        return rv

    @internalcode
    def __call__(self):
        if not self._context.environment.is_async:
            return original_call(self)
        else:
            return async_call(self)

    return update_wrapper__call__original_call


def wrap_macro_invoke(original_invoke):

    @internalcode
    def async_invoke(self, arguments, autoescape):
        rv = await (self._func(*arguments))
        if autoescape:
            rv = Markup(rv)
        return rv

    @internalcode
    def _invoke(self, arguments, autoescape):
        if not self._environment.is_async:
            return original_invoke(self, arguments, autoescape)
        else:
            return async_invoke(self, arguments, autoescape)

    return update_wrapper_invokeoriginal_invoke


@internalcode
def get_default_module_async(self):
    if self._module is not None:
        return self._module
    else:
        self._module = rv = await (self.make_module_async())
        return rv


def wrap_default_module(original_default_module):

    @internalcode
    def _get_default_module(self):
        if self.environment.is_async:
            raise RuntimeError('Template module attribute is unavailable in async mode')
        return original_default_module(self)

    return _get_default_module


def make_module_async--- This code section failed: ---

 134       0  LOAD_FAST                'self'
           2  LOAD_ATTR                'new_context'
           4  LOAD_FAST                'vars'
           6  LOAD_FAST                'shared'
           8  LOAD_FAST                'locals'
          10  CALL_FUNCTION_3       3  ''
          12  STORE_FAST               'context'

 135      14  BUILD_LIST_0          0  ''
          16  STORE_FAST               'body_stream'

 136      18  SETUP_LOOP           86  'to 86'
          20  LOAD_FAST                'self'
          22  LOAD_ATTR                'root_render_func'
          24  LOAD_FAST                'context'
          26  CALL_FUNCTION_1       1  ''
          28  GET_AITER        
          30  LOAD_CONST               None
          32  YIELD_FROM       
          34  SETUP_EXCEPT         48  'to 48'
          36  GET_ANEXT        
          38  LOAD_CONST               None
          40  YIELD_FROM       
          42  STORE_FAST               'item'
          44  POP_BLOCK        
          46  JUMP_FORWARD         70  'to 70'
        48_0  COME_FROM_EXCEPT     34  '34'
          48  DUP_TOP          
          50  LOAD_GLOBAL              'StopAsyncIteration'
          52  COMPARE_OP               'exception-match'
          54  POP_JUMP_IF_FALSE    68  'to 68'
          56  POP_TOP          
          58  POP_TOP          
          60  POP_TOP          
          62  POP_EXCEPT       
          64  POP_BLOCK        
          66  JUMP_ABSOLUTE        86  'to 86'
          68  END_FINALLY      
        70_0  COME_FROM            46  '46'

 137      70  LOAD_FAST                'body_stream'
          72  LOAD_ATTR                'append'
          74  LOAD_FAST                'item'
          76  CALL_FUNCTION_1       1  ''
          78  POP_TOP          
          80  JUMP_BACK            34  'to 34'
          82  POP_BLOCK        
          84  JUMP_ABSOLUTE        86  'to 86'
        86_0  COME_FROM_LOOP       18  '18'

 138      86  LOAD_GLOBAL              'TemplateModule'
          88  LOAD_FAST                'self'
          90  LOAD_FAST                'context'
          92  LOAD_FAST                'body_stream'
          94  CALL_FUNCTION_3       3  ''
          96  RETURN_VALUE     
          -1  RETURN_LAST      

Parse error at or near `JUMP_ABSOLUTE' instruction at offset 84


def patch_template():
    from jinja2 import Template
    Template.generate = wrap_generate_func(Template.generate)
    Template.generate_async = update_wrappergenerate_asyncTemplate.generate_async
    Template.render_async = update_wrapperrender_asyncTemplate.render_async
    Template.render = wrap_render_func(Template.render)
    Template._get_default_module = wrap_default_module(Template._get_default_module)
    Template._get_default_module_async = get_default_module_async
    Template.make_module_async = update_wrappermake_module_asyncTemplate.make_module_async


def patch_runtime():
    from jinja2.runtime import BlockReference, Macro
    BlockReference.__call__ = wrap_block_reference_call(BlockReference.__call__)
    Macro._invoke = wrap_macro_invoke(Macro._invoke)


def patch_filters():
    from jinja2.filters import FILTERS
    from jinja2.asyncfilters import ASYNC_FILTERS
    FILTERS.update(ASYNC_FILTERS)


def patch_all():
    patch_template()
    patch_runtime()
    patch_filters()


async def auto_await(value):
    if inspect.isawaitable(value):
        return await value
    else:
        return value


def auto_aiter--- This code section failed: ---

 182       0  LOAD_GLOBAL              'hasattr'
           2  LOAD_FAST                'iterable'
           4  LOAD_CONST               '__aiter__'
           6  CALL_FUNCTION_2       2  ''
           8  POP_JUMP_IF_FALSE    72  'to 72'

 183      10  SETUP_LOOP           68  'to 68'
          12  LOAD_FAST                'iterable'
          14  GET_AITER        
          16  LOAD_CONST               None
          18  YIELD_FROM       
          20  SETUP_EXCEPT         34  'to 34'
          22  GET_ANEXT        
          24  LOAD_CONST               None
          26  YIELD_FROM       
          28  STORE_FAST               'item'
          30  POP_BLOCK        
          32  JUMP_FORWARD         56  'to 56'
        34_0  COME_FROM_EXCEPT     20  '20'
          34  DUP_TOP          
          36  LOAD_GLOBAL              'StopAsyncIteration'
          38  COMPARE_OP               'exception-match'
          40  POP_JUMP_IF_FALSE    54  'to 54'
          42  POP_TOP          
          44  POP_TOP          
          46  POP_TOP          
          48  POP_EXCEPT       
          50  POP_BLOCK        
          52  JUMP_ABSOLUTE        68  'to 68'
          54  END_FINALLY      
        56_0  COME_FROM            32  '32'

 184      56  LOAD_FAST                'item'
          58  YIELD_VALUE      
          60  POP_TOP          
          62  JUMP_BACK            20  'to 20'
          64  POP_BLOCK        
          66  JUMP_ABSOLUTE        68  'to 68'
        68_0  COME_FROM_LOOP       10  '10'

 185      68  LOAD_CONST               None
          70  RETURN_END_IF    

 186      72  SETUP_LOOP           92  'to 92'
          74  LOAD_FAST                'iterable'
          76  GET_ITER         
          78  FOR_ITER             90  'to 90'
          80  STORE_FAST               'item'

 187      82  LOAD_FAST                'item'
          84  YIELD_VALUE      
          86  POP_TOP          
          88  JUMP_BACK            78  'to 78'
          90  POP_BLOCK        
        92_0  COME_FROM_LOOP       72  '72'

Parse error at or near `JUMP_ABSOLUTE' instruction at offset 66


class AsyncLoopContext(LoopContextBase):

    def __init__(self, async_iterator, undefined, after, length, recurse=None, depth0=0):
        LoopContextBase.__init__(self, undefined, recurse, depth0)
        self._async_iterator = async_iterator
        self._after = after
        self._length = length

    @property
    def length(self):
        if self._length is None:
            raise TypeError('Loop length for some iterators cannot be lazily calculated in async mode')
        return self._length

    def __aiter__(self):
        return AsyncLoopContextIterator(self)


class AsyncLoopContextIterator(object):
    __slots__ = ('context', )

    def __init__(self, context):
        self.context = context

    def __aiter__(self):
        return self

    async def __anext__(self):
        ctx = self.context
        ctx.index0 += 1
        if ctx._after is _last_iteration:
            raise StopAsyncIteration()
        ctx._before = ctx._current
        ctx._current = ctx._after
        try:
            ctx._after = await (ctx._async_iterator.__anext__())
        except StopAsyncIteration:
            ctx._after = _last_iteration

        return (ctx._current, ctx)


def make_async_loop_context(iterable, undefined, recurse=None, depth0=0):
    try:
        length = len(iterable)
    except (TypeError, AttributeError):
        if not hasattriterable'__aiter__':
            iterable = tuple(iterable)
            length = len(iterable)
        else:
            length = None

    async_iterator = auto_aiter(iterable)
    try:
        after = await (async_iterator.__anext__())
    except StopAsyncIteration:
        after = _last_iteration

    return AsyncLoopContext(async_iterator, undefined, after, length, recurse, depth0)